/**
 * Copyright (C), 2015-2019, bogewang有限公司
 * FileName: VerificaCodeServiceImpl
 * Author:   bogewang
 * Date:     2019/9/7 13:40
 * Description: 微信验证码接口实现
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bogewang.weixin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bogewang.core.base.BaseApiService;
import com.bogewang.core.base.BaseResponse;
import com.bogewang.core.constants.Constants;
import com.bogewang.core.core.utils.RedisUtil;
import com.bogewang.weixin.service.VerificaCodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈微信验证码接口实现〉
 *
 * @author bogewang
 * @create 2019/9/7
 * @since 1.0.0
 */
@RestController
public class VerificaCodeServiceImpl extends BaseApiService<JSONObject> implements VerificaCodeService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BaseResponse<JSONObject> verificaWeixinCode(String phone, String weixinCode) {
        // 1.验证手机号和验证码是否为空;
        if (StringUtils.isEmpty(phone)) {
            return setResultError("手机号不能为空!");
        }

        if (StringUtils.isEmpty(weixinCode)) {
            return setResultError("微信验证码不能为空!");
        }

        // 2.验证注册码;
        String keyPhone = Constants.WEIXINCODE_KEY + phone;
        String valueCode = redisUtil.getString(keyPhone);
        if (!weixinCode.equals(valueCode)){
            return setResultError("微信验证码不正确!");
        }

        // 3.移除注册码;
        redisUtil.delKey(keyPhone);

        // 4.返回
        return setResultSuccess("验证成功!");
    }
}